package com.xxhui.pagetransformerdemo;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import com.xxhui.pagetransformerdemo.PageTransformer.DepthPageTransformer;
import com.xxhui.pagetransformerdemo.PageTransformer.RotateDownPageTransformer;
import com.xxhui.pagetransformerdemo.PageTransformer.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;

    private int imageRes[] = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d};//如果找不到图请看看自己手机分辨率，我在mipmap值弄了一个xhdpi，其他分辨率复制到相应文件夹下，没报错掠过

    private List<ImageView> viewList = new ArrayList<ImageView>();

    private Menu menu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        getViewList();
        viewPager = (ViewPager) findViewById(R.id.viewPage);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageRes.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }
        });
    }

    private void getViewList(){
        int size  = imageRes.length;
        for(int i=0 ; i<size ; i++){
            ImageView imageView = new ImageView(MainActivity.this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//XY按比例拉伸到屏幕大小
            imageView.setImageResource(imageRes[i]);
            viewList.add(imageView);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,1,1,"DepthPageTransformer");
        menu.add(0,2,2," RotateDownPageTransformer");
        menu.add(0,3,3,"ZoomOutPageTransformer");
        this.menu = menu;
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                viewPager.setPageTransformer(true,new DepthPageTransformer());
                break;
            case 2:
                viewPager.setPageTransformer(true , new RotateDownPageTransformer());
                break;
            case 3:
                viewPager.setPageTransformer(true , new ZoomOutPageTransformer());
                break;
            default:
                break;
        }
        menu.setGroupEnabled(0,false);
        return true;
    }

}
